﻿#include "RemoteDialog.h"

RemoteDialog::RemoteDialog(QWidget *parent)
    : QDialog(parent),
      remoteConnectionRadioButton(new QRadioButton("远程连接")),
      hostEdit(new QLineEdit),
      portEdit(new QLineEdit),
      remoteDbNameEdit(new QLineEdit),
      usernameEdit(new QLineEdit),
      passwordEdit(new QLineEdit),
      remoteLabel1(new QLabel("主机")),
      remoteLabel2(new QLabel("端口")),
      remoteLabel3(new QLabel("数据库名称")),
      remoteLabel4(new QLabel("用户账号")),
      remoteLabel5(new QLabel("密码")),
      connectButton(new QPushButton("确定连接")),
      cancelButton(new QPushButton("取消"))
{
    setWindowTitle("远程连接");
    QVBoxLayout *mainLayout = new QVBoxLayout(this);

    // Update the stylesheet to further increase the width of the QLineEdit
    setStyleSheet(
        "QLineEdit, QDateEdit {"
        "  font-size: 18px;"
        "  font-weight: bold;"
        "  border: 2px solid #E0E0E0;"
        "  border-radius: 20px;"
        "  padding: 10px;"
        "  color: gray;"
        "  width: 400px;"  // 增加编辑框的长度
        "}"
        "QPushButton {"
        "  font-size: 18px;"
        "  font-weight: bold;"
        "  border: 2px solid #E0E0E0;"
        "  border-radius: 20px;"
        "  padding: 10px;"
        "  background-color: #0093D1;"
        "  color: white;"
        "}");

    mainLayout->addWidget(remoteConnectionRadioButton);

    // Create a grid layout for labels and edit boxes
    QGridLayout *gridLayout = new QGridLayout;

    gridLayout->addWidget(remoteLabel1, 0, 0);
    gridLayout->addWidget(hostEdit, 0, 1);

    gridLayout->addWidget(remoteLabel2, 1, 0);
    gridLayout->addWidget(portEdit, 1, 1);

    gridLayout->addWidget(remoteLabel3, 2, 0);
    gridLayout->addWidget(remoteDbNameEdit, 2, 1);

    gridLayout->addWidget(remoteLabel4, 3, 0);
    gridLayout->addWidget(usernameEdit, 3, 1);

    gridLayout->addWidget(remoteLabel5, 4, 0);
    gridLayout->addWidget(passwordEdit, 4, 1);

    mainLayout->addLayout(gridLayout);

    QHBoxLayout *buttonLayout = new QHBoxLayout;
    buttonLayout->addStretch(1);
    buttonLayout->addWidget(connectButton);
    buttonLayout->addWidget(cancelButton);
    mainLayout->addLayout(buttonLayout);

    connect(remoteConnectionRadioButton, &QRadioButton::toggled, this, &RemoteDialog::onFieldsChanged);
    connect(connectButton, &QPushButton::clicked, this, &RemoteDialog::accept);
    connect(cancelButton, &QPushButton::clicked, this, &RemoteDialog::reject);
    connect(hostEdit, &QLineEdit::textChanged, this, &RemoteDialog::onFieldsChanged);
    connect(portEdit, &QLineEdit::textChanged, this, &RemoteDialog::onFieldsChanged);
    connect(remoteDbNameEdit, &QLineEdit::textChanged, this, &RemoteDialog::onFieldsChanged);
    connect(usernameEdit, &QLineEdit::textChanged, this, &RemoteDialog::onFieldsChanged);
    connect(passwordEdit, &QLineEdit::textChanged, this, &RemoteDialog::onFieldsChanged);

    setLayout(mainLayout);

    // Increase the width of the dialog
    setFixedWidth(500);  // 增加界面的宽度
}

QString RemoteDialog::getHost() const
{
    return hostEdit->text();
}

QString RemoteDialog::getPort() const
{
    return portEdit->text();
}

QString RemoteDialog::getRemoteDbName() const
{
    return remoteDbNameEdit->text();
}

QString RemoteDialog::getUsername() const
{
    return usernameEdit->text();
}

QString RemoteDialog::getPassword() const
{
    return passwordEdit->text();
}

void RemoteDialog::onFieldsChanged()
{
    connectButton->setEnabled(!hostEdit->text().isEmpty() &&
                              !portEdit->text().isEmpty() &&
                              !remoteDbNameEdit->text().isEmpty() &&
                              !usernameEdit->text().isEmpty() &&
                              !passwordEdit->text().isEmpty());
}
